var console = console;

homeApp.controller('apk1Controller', ['$scope', '$http', '$filter', '$modal', function ($scope, $http, $filter, $modal) {
    'use strict';
    
    $scope.maxSize          = 3;
    $scope.bigCurrentPage   = 1;
    
    $scope.totalstreams     = 10;
    
    $scope.nodata   = true;
    $scope.loading  = true;
    
    $scope.streams = [];
    
    $http.get('api/gangguan')
        .success(function (data) {
            //console.log(data);
            if (data.response === "OK" && data.message === "Get all gangguan data success.") {
                
                $scope.datagangguan    = data.result.gangguan;
                
                $scope.streams          = $scope.datagangguan.slice(0, $scope.totalstreams);
            } else {
                console.log(data);
            }
            $scope.loading  = false;
        }).error(function (data) {
            $scope.loading  = false;
            console.log(data);
        });

    $scope.$watch('bigCurrentPage', function (newVal, oldVal) {
        if ((newVal !== oldVal) && (typeof(newVal) !== 'undefined') && (typeof(oldVal) !== 'undefined')) {
            //console.log($scope.bigCurrentPage);
            $scope.streams  = $scope.datagangguan.slice(($scope.bigCurrentPage - 1) * $scope.totalstreams, $scope.bigCurrentPage * $scope.totalstreams);
        }
    });
    
    $scope.$watch('streams', function (newVal, oldVal) {
        if ((newVal !== oldVal) && (typeof(newVal) !== 'undefined') && (typeof(oldVal) !== 'undefined')) {
            if (newVal.length > 0) {
                $scope.nodata = false;   
            } else {
                $scope.nodata = true;
            }
        }
    });
    
    $scope.makeTanggal = function(some) {
        return Date.create(some).format('{dd} {Mon} {yyyy}') + "\n" + Date.create(some).format('{hh}:{mm}');
    }
    $scope.tryingtoexport = function(some) {
        return JSON.stringify(some);
    }        
    $scope.clickfoto = function(some) {
        var modalInstance = $modal.open({
            templateUrl: 'FotoPopUp.html',
            controller: 'PopUpFotoController',
            backdropClass : 'bacdropAdditionClass',
            size: 'lg',
            resolve: {
                params: function () {
                    return 'files/photos/' + some;
                }
            }
        });
    }
    
    $scope.clickdelete = function(some) {
        //console.log($scope.streams);
        var modalInstance = $modal.open({
            templateUrl: 'DeletePopUp.html',
            controller: 'PopUpDeleteController',
            backdropClass : 'bacdropAdditionClass',
            windowClass : 'windowAdditionClass',
            size: 'sm',
            resolve: {
                params: function () {
                    return some;
                }
            }
        });
        
        modalInstance.result.then(function (anotherparams) {
//            var maxPage = Math.ceil($scope.datagangguan.length / $scope.totalstreams);
            
            $scope.datagangguan = _.reject($scope.datagangguan, function(stream) {
                return (stream.id == anotherparams);
            });
            
//            if ($scope.bigCurrentPage === maxPage) {
//                if (($scope.bigCurrentPage * $scope.totalstreams) <= $scope.datagangguan.length) {
//                    $scope.bigCurrentPage -= 1;
//                }
//            }
            $scope.streams  = $scope.datagangguan.slice(($scope.bigCurrentPage - 1) * $scope.totalstreams, $scope.bigCurrentPage * $scope.totalstreams);
        }, function () {
            
        });
    }
    
}]);

homeApp.controller('PopUpFotoController',['$scope','$modal','params', function($scope,$modal,params) {
    $scope.params = params;
}]);
homeApp.controller('PopUpDeleteController',['$scope','$modal','params','$modalInstance','$http',function($scope,$modal,params,$modalInstance,$http) {
    $scope.disabled = false;
    $scope.ok = function () {
        $scope.disabled = true;
        $http.get('api/deletegangguan',{params:{id : params}})
            .success(function (data) {
                //console.log(data);
                if (data.response === "OK" && data.status_code == 200) {
                    $modalInstance.close(params);
                } else {
                    console.log(data);
                }
            }).error(function (data) {
                console.log(data);
            });
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };    
}]);

homeApp.directive('bodywatcher', function () {
    'use strict';
    return {
        link: function( scope, elem, attrs ) {
            scope.$watch(function() {
                if (elem.height() > elem[0].parentElement.offsetHeight) {
                    scope.__checker = true;
                } else {
                    scope.__checker = false;
                }
                _.defer(function(){scope.$apply();});
            });
        }
    }
});

homeApp.directive('headmover', function() {
    'use strict';

    return {
        link: function (scope, element, attrs) {
            scope.$watch (
                function() {
                    return scope.__checker;
                }, 
                function(val) {
                    if (val) {
                        element.attr('style', 'width : 76px');
                    } else {
                        element.attr('style', 'width : 60px');
                    }
            }); // */
        }
    };
});

homeApp.directive('myRepeatDirective', function () {
    'use strict';
    return function (scope, element, attrs) {
        if (scope.$last) {
            $(".table-body-apk1").scroll(function () {
                $(".table-header-apk1").offset({ left: (-1 * this.scrollLeft) + 41 });
            });
        }
    };
});

